/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ECovoiturage. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.ui.page;

import com.smouquet.ecovoiturage.ApplicationContext;
import com.smouquet.ecovoiturage.core.domain.Entreprise;
import com.smouquet.ecovoiturage.core.domain.Utilisateur;
import com.smouquet.ecovoiturage.core.service.EntrepriseService;
import com.smouquet.ecovoiturage.core.service.UtilisateurService;
import com.smouquet.ecovoiturage.ui.NotificationUtil;
import com.smouquet.ecovoiturage.ui.page.RegistrationForm.CreateAccountEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import java.util.List;

/**
 * Vue pour la création d'un compte utilisateur.
 * 
 * @author sebastien.mouquet
 */
public class RegistrationView extends VerticalLayout implements View,
                                      RegistrationForm.CreateAccountListener
{
    public static final String VIEW_NAME = "registration";
    
    
    private Window winRegistration;
    
    public RegistrationView()
    {
        setSizeFull();
    }
    

    @Override
    public void enter(ViewChangeEvent event)
    {
        EntrepriseService service = ApplicationContext.getInstance(EntrepriseService.class);
        List<Entreprise> entreprises = service.getAllOrderByNom();
        
        RegistrationForm registrationForm = new RegistrationForm(entreprises);
        registrationForm.addCreateAccountListener(this);
        
        VerticalLayout layout = new VerticalLayout();
        layout.setSizeFull();
        layout.setMargin(false);
        layout.addComponent(registrationForm);
        layout.setComponentAlignment(registrationForm, Alignment.MIDDLE_CENTER);
        
        winRegistration = new Window("ECovoiturage (1.0.M2)");
        winRegistration.setWidth(450, Unit.PIXELS);
        winRegistration.setHeight(300, Unit.PIXELS);
        winRegistration.setModal(true);
        winRegistration.setClosable(false);
        winRegistration.setResizable(false);
        winRegistration.setContent(registrationForm);
        
        for(Window w : getUI().getWindows()) {
            getUI().removeWindow(w);
        }
        getUI().addWindow(winRegistration);
    }
    

    @Override
    public void onCreateAccount(CreateAccountEvent event)
    {
        UtilisateurService service = ApplicationContext.getInstance(UtilisateurService.class);
        
        service.add(event.getUtilisateur());
        
        Utilisateur u = service.connexion(event.getUtilisateur().getEmail(), event.getUtilisateur().getMdp());
        
        if(u == null)
        {
            NotificationUtil.showWarningNotification("Identifiant et/ou mot de passe incorrect");
        }
        else
        {
            // On enregistre l'utilisateur au niveau de la session
            getSession().setAttribute(Utilisateur.class, u);
            
            // Fermeture de la fenêtre et
            // démarrage de l'application
            winRegistration.close();
            ((AbstractUI)getUI()).initialize();
        }
    }
    
}
